package com.ghc.androidexample.share;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.ghc.androidexample.R;
import com.ghc.androidexample.R.id;
import com.ghc.androidexample.R.layout;
import com.ghc.androidexample.R.menu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.content.FileProvider;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

public class FileSelectorActivity extends Activity {
	private static final String FILE_FOUND = "Found %d file(s)";
	private static final String FILE_SELECT = "Select: %s";
	
	private ListView mLvFile;
	private ProgressDialog mProgressDialog;
	private Intent mResultIntent;
	private List<String> mFileNames;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_file_selector);
		setResult(Activity.RESULT_CANCELED, null);

		// Set up an Intent to send back to apps that request a file
		mResultIntent = new Intent("com.ghc.androidexample.ACTION_RETURN_FILE");

		mLvFile = (ListView) findViewById(R.id.lvFileSelector);
		mLvFile.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long rowId) {
				File file = new File(mFileNames.get(position));
				try {
					Uri uri = FileProvider.getUriForFile(
							FileSelectorActivity.this,
							"com.ghc.androidexample.fileprovider", file);
					if (uri != null) {
						mResultIntent
								.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
						mResultIntent.setDataAndType(uri, getContentResolver()
								.getType(uri));
						FileSelectorActivity.this.setResult(RESULT_OK,
								mResultIntent);
					} else {
						mResultIntent.setDataAndType(null, "");
						FileSelectorActivity.this.setResult(RESULT_CANCELED,
								mResultIntent);
					}
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				}
				TextView tvSelect = (TextView)findViewById(R.id.tvFileSelect);
				tvSelect.setText(String.format(FILE_SELECT, file.getName()));
			}
		});

		processFile();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.file_selector, menu);
		return true;
	}

	private void processFile() {
		mProgressDialog = new ProgressDialog(this);
		mProgressDialog.show();
		new ProcessFileTask().execute();
	}

	private class ProcessFileTask extends
			AsyncTask<String, List<String>, List<String>> {

		@Override
		protected List<String> doInBackground(String... params) {
			List<String> fileNames = new ArrayList<String>();
			File file = new File(getFilesDir(), "images");
			File[] files = file.listFiles();
			if (files != null) {
				for (File f : files) {
					fileNames.add(f.getPath());
				}
			}
			return fileNames;
		}

		@Override
		protected void onPostExecute(List<String> result) {
			mFileNames = result;
			TextView tvCount = (TextView) findViewById(R.id.tvFileCount);
			tvCount.setText(String.format(FILE_FOUND, mFileNames.size()));

			// Add to listView
			FileSelectorAdapter adapter = new FileSelectorAdapter(
					FileSelectorActivity.this,
					R.layout.activity_file_selector_item,
					mFileNames.toArray(new String[] {}));
			mLvFile.setAdapter(adapter);
			adapter.notifyDataSetChanged();

			mProgressDialog.dismiss();
		}
	}

	public void onDoneClick(View v) {
		finish();
	}
}
